package com.example.administrator.xtu_community.Adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.administrator.xtu_community.R;
import com.example.administrator.xtu_community.activity.DetailsActivity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.example.administrator.xtu_community.bean.Note;
import com.example.administrator.xtu_community.simpleClass.Video;
import com.example.administrator.xtu_community.utils.GlideUtils;
import com.example.administrator.xtu_community.utils.Tuijian;

import org.litepal.crud.DataSupport;

public class HomeRecyclerViewAdapter extends RecyclerView.Adapter<HomeRecyclerViewAdapter.ViewHolder> {

    private Context mContext;

    private final List<Note> mVideoList = new ArrayList<>();

    public HomeRecyclerViewAdapter() {

    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        CardView cardView;
        ImageView video_picture;
        TextView video_description;
        ImageView head_portrait;
        TextView username;
        ImageView love;
        TextView love_amount;

        public ViewHolder(View view) {
            super(view);
            cardView = view.findViewById(R.id.home_cardview);
            video_picture = view.findViewById(R.id.home_main_image);
            video_description = view.findViewById(R.id.home_title);
            head_portrait = view.findViewById(R.id.home_head_image);
            username = view.findViewById(R.id.home_id);
            love = view.findViewById(R.id.love);
            love_amount = view.findViewById(R.id.love_amount);
        }

    }
    public void setmVideoList(List<Note> list) {
        if (!mVideoList.isEmpty()) {
            mVideoList.clear();
        }
        mVideoList.addAll(list);
        Collections.sort(mVideoList, new Comparator<Note>() {
            @Override
            public int compare(Note note, Note t1) {
                if(Tuijian.ins().getMap().get(note.getType()) > Tuijian.ins().getMap().get(t1.getType()))
                    return -1;
                else return 1;
            }
        });
    }

    public HomeRecyclerViewAdapter(List<Note> videoList) {
        mVideoList.clear();
        mVideoList.addAll(videoList);
        Collections.sort(mVideoList, new Comparator<Note>() {
            @Override
            public int compare(Note note, Note t1) {
                if(Tuijian.ins().getMap().get(note.getType()) > Tuijian.ins().getMap().get(t1.getType()))
                    return -1;
                else return 1;
            }
        });
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (mContext==null) {
            mContext = parent.getContext();
        }
        View view = LayoutInflater.from(mContext).inflate(R.layout.recycler_view_homepage,
                parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Note video = mVideoList.get(position);
        if(video.getNote_from() != 2) {
            GlideUtils.loadImage(mContext,video.getMain_image(), holder.video_picture);
        } else {
            GlideUtils.loadImage(mContext,Uri.parse(video.getImage2()), holder.video_picture);
        }
        GlideUtils.loadImage(mContext, R.drawable.girl,holder.head_portrait);
        holder.video_description.setText(video.getTitle());
        holder.username.setText("一只🍄");
        holder.love_amount.setText("100");
        holder.love.setImageDrawable(mContext.getResources().getDrawable(R.drawable.love));
        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position1 = holder.getBindingAdapterPosition();
                Note video1 = mVideoList.get(position1);
                if(video1.getType() != null)
                    Tuijian.ins().add(video1.getType());
                Intent intent = new Intent(mContext, DetailsActivity.class);
                intent.putExtra("note",video1);
                mContext.startActivity(intent);
            }
        });
    }
    @Override
    public int getItemCount() {
        return mVideoList.size();
    }

    public void refresh() {
        mVideoList.clear();
        mVideoList.addAll(DataSupport.findAll(Note.class,true));
        Collections.sort(mVideoList, new Comparator<Note>() {
            @Override
            public int compare(Note note, Note t1) {
                if(!Tuijian.ins().getMap().containsKey(note.getType())) {
                    return 0;
                }
                if(Tuijian.ins().getMap().get(note.getType()) > Tuijian.ins().getMap().get(t1.getType()))
                    return -1;
                else return 1;
            }
        });
        notifyDataSetChanged();
    }
}
